
package com.compuvest.feed

class BookmakerFeed {

    def static final BOOKMAKER_LINK = 'http://10.0.1.93/Sentinel/ReadData.ashx?Sw=20'

    def updatePrice(items) {

        def code, action, result
        items.each { accwareid, item ->
            code = item.status['code']
            action = item.action['type']

            if((code == '200' || code == '201') && action != 'delete') {

                result = savePrice(accwareid, item.field['sellprice'], item.field['reslprice'])
                if(result != 'Error') {
                    item.status['accware'] = 'ok'
                }
            }
        }
    }

    def savePrice(accwareid, sellprice, reslprice) {

        def url = new URL(BOOKMAKER_LINK + "&SKUBM=${accwareid}&PRC=${sellprice}&DPRC=${reslprice}")

        def connection = url.openConnection()
        connection.setRequestMethod("GET")
        connection.doOutput = true

        if(connection.responseCode == 200 || connection.responseCode == 201) {
            return connection.content.text
        }
        println 'savePrice() error occurred: ' << connection.responseCode << ' : ' << connection.responseMessage

        return 'Error'
    }

}

